package com.zzptc.myk.myapplication;

import androidx.appcompat.app.AppCompatActivity;

import android.content.Intent;
import android.content.SharedPreferences;
import android.os.Bundle;
import android.text.Editable;
import android.view.View;
import android.widget.Button;
import android.widget.CompoundButton;
import android.widget.EditText;
import android.widget.Switch;
import android.widget.Toast;

import com.zzptc.myk.myapplication.entity.User;

import java.util.ArrayList;
import java.util.List;

public class MainActivity extends AppCompatActivity {
    EditText et_myk_name,et_myk_pwd;
    Switch aSwitch_myk;
    Button btn_myk_login,btn_myk_reg;
    boolean flag;//全局变量
    List<User> list=new ArrayList<>();



    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        init();
        eventListenr();
        User user=new User();
        User user1=new User("admin","123");
        User user2=new User("aaa","123");
        User user3=new User("bbb","123");
        list.add(user1);
        list.add(user2);
        list.add(user3);
    }
    private void eventListenr() {

        btn_myk_reg.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                Intent intent=new Intent(MainActivity.this,RegActivity.class);
                startActivityForResult(intent,1);

            }
        });
        //自动登录是否被改变
        aSwitch_myk.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
            @Override
            public void onCheckedChanged(CompoundButton compoundButton, boolean b) {
                if(b){
                    flag=true;
                }else {
                    flag=false;
                }
            }
        });
        btn_myk_login.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                //获取值  用户名，密码
                String name = et_myk_name.getText().toString().trim();
                String pwd = et_myk_pwd.getText().toString().trim();
                SharedPreferences sh = getSharedPreferences("one", MODE_PRIVATE);
                SharedPreferences.Editor edit = sh.edit();
                if(flag){
                    //记住  用户名，密码，是否自动登录
                    edit.putString("uname",name);
                    edit.putString("password",pwd);
                    edit.putBoolean("autologin",flag);
                    edit.commit();
                }else {
                    edit.putString("uname","");
                    edit.putString("password","");
                    edit.putBoolean("autologin",false);
                    edit.commit();
                }
                //登录
                login(name,pwd);
                btn_myk_login.setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View v) {
                        Intent intent=new Intent(MainActivity.this,LoginActivity.class);
                        startActivityForResult(intent,1);
                    }
                });
            }
        });

    }

    public  void  login(String name,String pwd){
        boolean isLogin=false;
        //循环
        for(User u:list){
            if(u.getUsername().equals(name)&&u.getPassword().equals(pwd)){
            //找到用户
            isLogin=true;
            break;
            }else {
                isLogin=false;
            }
        }
        if(isLogin){

            Toast.makeText(this, "登录成功", Toast.LENGTH_SHORT).show();
        }else {
            Toast.makeText(this, "登录失败", Toast.LENGTH_SHORT).show();
        }

    }

    private void init() {
        et_myk_name=findViewById(R.id.et_myk_name);
        et_myk_pwd=findViewById(R.id.et_myk_pwd);
        aSwitch_myk=findViewById(R.id.switch_auto_login);
        btn_myk_login=findViewById(R.id.btn_myk_login);
        btn_myk_reg=findViewById(R.id.btn_myk_reg);
    }
}
